<?php
require_once("../include/config.php");
require_once('../include/redis/redis.common.class.php');
require_once('../include/redis/redis.archives.class.php');
require_once('include/Mumayi.mysql.class.php');
require_once('include/Developer.mysql.class.php');

//取到最终页，所有AJAX数据，一次性处理相关数据结果
$sid= (int)$_REQUEST['sid'];
$dataArr = array();
$exptime = 3600*3;
$nowtimes = time();
//a.取到有多少人评分
//处理评分逻辑
$numrow = $DB->fetch_first("select typeid,softrank,scoretotal,scorenum FROM `dede_addonsoft` where aid=$sid ");
//a.先取编辑评分*10得到总分
$edittotal=trim($numrow['softrank'])*10;

$typeid = $numrow['typeid'];


//b.再取到网友评分
$othertotal = trim($numrow['scoretotal']);
$softrankbigs = (int)(($edittotal+$othertotal)/($numrow['scorenum']+10));
$score = $softrankbigs*10;
if($score > 50) $score=50; 
if($score < 10) $score=10; 
$dataArr['scroeNum'] = $numrow['scorenum']+10;

if($score<=10){$scoretext='不给力';}
if($score>10&&$score<=20){$scoretext='一般般';}
if($score>20&&$score<=30){$scoretext='一般';}
if($score>30&&$score<=40){$scoretext='还不错';}
if($score>40){$scoretext='强烈推荐';}

$dataArr['scroeText'] = $scoretext;




//b.取到有多少下载量
$dedeArchives=new DedeArchives();
$total=$dedeArchives->getDownTotal($sid);
if(empty($total)) $total= 0;
$dataArr['downNum'] = FormatDownNum($total);


//c.取到右侧9个推荐应用
$typeid = $numrow['typeid'];

$filenames = CACHE_DIR.'/newaddsoftlist_'.$typeid.'.txt';
$newtime = (time()-filemtime($filenames));
//缓存ID
$redis_id="newaddsoftlist1";
$dedeCommon = new DedeCommon();

$data = array();
$result=$dedeCommon->addGet($redis_id);
$listhtml = '';
//如果过期，重新生成一次，再写入缓存
$result = '';
if($result == ''){
	/*$sql=" Select
		dede_addonsoft.*,
		dede_archives.*
		From dede_archives
		left join dede_addonsoft on dede_addonsoft.aid=dede_archives.id
		WHERE dede_archives.arcrank =0 AND dede_archives.channel=3 AND dede_archives.ext_attribute LIKE '%c%' order by rand() limit 0,9 ";

	$sql=" Select `id`,`title`,`version`,`channel`,`pubdate`,`arcrank`,`softrank`,`litpic`,`litpicH`,`aid`,marketsort 
		From dede_archives
		left join dede_addonsoft on dede_addonsoft.aid=dede_archives.id
		WHERE dede_archives.arcrank =0 AND dede_archives.channel=3 AND dede_archives.ext_attribute LIKE '%c%' order by softsort  desc limit 0,9 ";
	*/

	
	$sql = "SELECT versionname,id,title,icon FROM `mumayi_soft` WHERE sid =0 AND status =2 AND adminstatus!=20 AND adminstatus!=20 AND ext_attribute LIKE '%c%' ORDER BY softsort DESC LIMIT 0 , 9";
	$query = $DEVDB->query($sql);
	while(@$softrow = $DEVDB->fetch_array($query)) {
			
			$baseIcon = dirname($softrow['icon']).'/litpicH_'.basename($softrow['icon']);
			$iconDir = $GLOBALS['UPLOAD_DIR'].$baseIcon;
			if(is_file($iconDir)){
				$showlogo = GetRandImages($baseIcon);
			}else{
				$showlogo = GetRandImages($softrow['icon']);
			}
			$versionname = $softrow['versionname'];
			$title = $softrow['title'];
			$id = $softrow['id'];
		
			$listhtml.='<li><a class="img72_72" title="'.$title.'V'.$versionname.'" target="_blank" href="http://www.mumayi.com/android-'.$id.'.html">
                              <img alt="'.$title.'V'.$versionname.'"  width="72" height="72" src="'.$showlogo.'">
                              <div class="fff72_72"></div>
                          </a><div class="tit50">
                              <a title="'.$title.'V'.$versionname.'" target="_blank" href="http://www.mumayi.com/android-'.$id.'.html">'.$title.'V'.$versionname.'</a>
                          </div>
                      </li>';

	}

	//写入redis,并设定时间
	$exptime = 2*3600;
	$dedeCommon->addTimeSet($redis_id,$exptime,$listhtml);

	$result=file_put_contents($filenames, $listhtml);
	@chmod($filenames, 0775);

}else{
	$listhtml = $dedeCommon->addGet($redis_id);
	
}

$dataArr['rightList'] = $listhtml;






//取到缓存ID
$mpkgamedateFile = "/mnt/html/shell/cache/mpkgamedate.txt";
$mpkgamedate_html = file_get_contents($mpkgamedateFile);
$allMpk = explode(',',$mpkgamedate_html);

//右侧推荐9个 MPK游戏
$rightID = implode(',',array_slice($allMpk, 0,9)); 


//缓存ID
$redis_id = "softrightmpk";
$data = array();
$result = $dedeCommon->addGet($redis_id);
$rightmpkhtml = '';
$result = '';
if($result == ''){
	$sql = "SELECT versionname,id,title,icon FROM `mumayi_soft` WHERE adminstatus=20 AND id IN ($rightID) ORDER BY id DESC LIMIT 0 , 9";
	$query = $DEVDB->query($sql);
	while(@$softrow = $DEVDB->fetch_array($query)) {
			
			$baseIcon = dirname($softrow['icon']).'/litpicH_'.basename($softrow['icon']);
			$iconDir = $GLOBALS['UPLOAD_DIR'].$baseIcon;
			if(is_file($iconDir)){
				$showlogo = GetRandImages($baseIcon);
			}else{
				$showlogo = GetRandImages($softrow['icon']);
			}
			$versionname = $softrow['versionname'];
			$title = $softrow['title'];
			$id = $softrow['id'];
			$rightmpkhtml.='<li><a class="img72_72" title="'.$title.'V'.$versionname.'" target="_blank" href="http://mpk.mumayi.com/'.$id.'.html">
								  <img alt="'.$title.'V'.$versionname.'"  width="72" height="72" src="'.$showlogo.'">
								  <div class="fff72_72"></div>
							  </a><div class="tit50">
								  <a title="'.$title.'V'.$versionname.'" target="_blank" href="http://mpk.mumayi.com/'.$id.'.html">'.$title.'V'.$versionname.'</a>
							  </div>
						  </li>';

			//写入redis,并设定时间
			$exptime = 2*3600;
			$dedeCommon->addTimeSet($redis_id,$exptime,$rightmpkhtml);
	}
}else{
	$rightmpkhtml = $dedeCommon->addGet($redis_id);
}

$dataArr['rightMpkList'] = $rightmpkhtml;


//底部侧推荐6个 MPK游戏
$bottomID = implode(',',array_slice($allMpk, 9,6)); 
$redis_id = "softbottommpk";
$data = array();
$result = $dedeCommon->addGet($redis_id);
$bottommpkhtml = '';
$result = '';
if($result == ''){
	$sql = "SELECT versionname,id,title,icon FROM `mumayi_soft` WHERE adminstatus=20 AND id IN ($bottomID) ORDER BY id DESC LIMIT 0 , 9";
	$query = $DEVDB->query($sql);
	while(@$softrow = $DEVDB->fetch_array($query)) {
			
			$baseIcon = dirname($softrow['icon']).'/litpicH_'.basename($softrow['icon']);
			$iconDir = $GLOBALS['UPLOAD_DIR'].$baseIcon;
			if(is_file($iconDir)){
				$showlogo = GetRandImages($baseIcon);
			}else{
				$showlogo = GetRandImages($softrow['icon']);
			}
			$versionname = $softrow['versionname'];
			$title = $softrow['title'];
			$id = $softrow['id'];
			
			$bottommpkhtml.='<li>
			  <a class="img72_72" title="'.$title.' V'.$versionname.'" target="_blank" href="http://mpk.mumayi.com/'.$id.'.html">
				  <img alt="'.$title.' V'.$versionname.'"  width="72" height="72" src="'.$showlogo.'">
				  <div class="fff72_72"></div>
			  </a>
			  <div class="tit50">
				  <a title="'.$title.' V'.$versionname.'" target="_blank" href="http://mpk.mumayi.com/'.$id.'.html">'.$title.' V'.$versionname.'</a>
			  </div>
		  </li>';

			//写入redis,并设定时间
			$exptime = 2*3600;
			$dedeCommon->addTimeSet($redis_id,$exptime,$bottommpkhtml);
	}
}else{
	$bottommpkhtml = $dedeCommon->addGet($redis_id);
}







//Head头部游戏推广应用
$nowtimes = time();
$newgamelist_web = "newgamelist_web";
$newgamelist_pc = "newgamelist_pc";
$gamelist_web_html = $dedeCommon->addGet($newgamelist_web);
$gamelist_pc_html = $dedeCommon->addGet($newgamelist_pc);


//如果过期，重新生成一次，再写入缓存
if($gamelist_web_html == ''){
	$webgamedateFile = "/mnt/html/shell/cache/webgamedate_web.txt";
	$pcgamedateFile = "/mnt/html/shell/cache/pcgamedate_pc.txt";
	
	

	$gamelist_web_html = @file_get_contents($webgamedateFile);
	$gamelist_pc_html = @file_get_contents($pcgamedateFile);

	$dedeCommon->addTimeSet($newgamelist_web,$exptime,$gamelist_web_html);
	$dedeCommon->addTimeSet($newgamelist_pc,$exptime,$gamelist_pc_html);

}

$dataArr['webgamedata'] = $gamelist_web_html;
$dataArr['pcgamedata'] = $gamelist_pc_html;





//d.取到头部滚动广告列表
$headpage = 1;
$filenames = CACHE_DIR.'/newheadscroll'.$headpage.'.txt';
$newtime = (time()-filemtime($filenames));
//头部分页
$redis_id = "newheadscroll".$headpage;
$result = $dedeCommon->addGet($redis_id);
$listhtml = '';
//如果过期，重新生成一次，再写入缓存
$result = '';
if($result == ''){
	$nowtime = time()-24*3600;
	if($headpage==1) {


		$sql = "SELECT versionname,id,title,icon FROM `mumayi_soft` WHERE sid =0 AND status =2 AND adminstatus!=20 AND adminstatus!=20 AND ext_attribute LIKE '%b%' ORDER BY rand()   LIMIT 0 , 8";
		$query = $DEVDB->query($sql);
		while(@$softrow = $DEVDB->fetch_array($query)) {
				
				$baseIcon = dirname($softrow['icon']).'/litpicH_'.basename($softrow['icon']);
				$iconDir = $GLOBALS['UPLOAD_DIR'].$baseIcon;
				if(is_file($iconDir)){
					$showlogo = GetRandImages($baseIcon);
				}else{
					$showlogo = GetRandImages($softrow['icon']);
				}
				$versionname = $softrow['versionname'];
				$title = $softrow['title'];
				$id = $softrow['id'];
				
				$listhtml.='<li>
				<a class="img72_72" title="'.$title.' V'.$versionname.'" target="_blank" href="http://www.mumayi.com/android-'.$id.'.html">
					<img alt="'.$title.' V'.$versionname.'"  width="72" height="72" src="'.$showlogo.'">
					<div class="green72_72"></div>
				</a>
				<div class="tit50">
					<a title="'.$title.' V'.$versionname.'" target="_blank" href="http://www.mumayi.com/android-'.$id.'.html">'.$title.'</a>
				</div>
			</li>';

		}

		
	}else{

		$sql = "SELECT versionname,id,title,icon FROM `mumayi_soft` WHERE sid =0 AND status =2 AND adminstatus!=20 AND adminstatus!=20 AND created>=$nowtime  ORDER BY rand()   LIMIT 0 , 8";
		$query = $DEVDB->query($sql);
		while(@$softrow = $DEVDB->fetch_array($query)) {
				
				$baseIcon = dirname($softrow['icon']).'/litpicH_'.basename($softrow['icon']);
				$iconDir = $GLOBALS['UPLOAD_DIR'].$baseIcon;
				if(is_file($iconDir)){
					$showlogo = GetRandImages($baseIcon);
				}else{
					$showlogo = GetRandImages($softrow['icon']);
				}
				$versionname = $softrow['versionname'];
				$title = $softrow['title'];
				$id = $softrow['id'];
				
				$listhtml.='<li>
				<a class="img72_72" title="'.$title.' V'.$versionname.'" target="_blank" href="http://www.mumayi.com/android-'.$id.'.html">
					<img alt="'.$title.' V'.$versionname.'"  width="72" height="72" src="'.$showlogo.'">
					<div class="green72_72"></div>
				</a>
				<div class="tit50">
					<a title="'.$title.' V'.$versionname.'" target="_blank" href="http://www.mumayi.com/android-'.$id.'.html">'.$title.'</a>
				</div>
			</li>';

		}
		
	}
	//写入redis,并设定时间
	$dedeCommon->addTimeSet($redis_id,$exptime,$listhtml);

	//写到本地文件
	$result=file_put_contents($filenames, $listhtml);
	@chmod($filenames, 0775);

}else{
	$listhtml = $dedeCommon->addGet($redis_id);
}

$dataArr['headList'] = $listhtml;


//e.取到你可能喜欢相关软件

$filenames = CACHE_DIR.'/newaddrecommendlist_'.$typeid.'.txt';
$newtime = (time()-filemtime($filenames));
//缓存ID
$redis_id="newaddrecommendlist".$typeid;
$dedeCommon = new DedeCommon();

$data = array();
$result = $dedeCommon->addGet($redis_id);
$recommendlisthtml = '';
//如果过期，重新生成一次，再写入缓存
$result = '';
if($result==''){
	//先取到此分类，随机相关软件
	$nowtimes = time()-24*3600;
	/*$sql=" Select
		dede_addonsoft.*,
		dede_archives.*
		From dede_archives
		left join dede_addonsoft on dede_addonsoft.aid=dede_archives.id
		where arcrank=0 AND dede_archives.pubdate<='$nowtimes' AND dede_archives.typeid = $typeid   ORDER BY  rand()  limit 0,6 ";*/
	
	$sql = "SELECT versionname,id,title,icon FROM `mumayi_soft` WHERE sid =0 AND status =2 AND  adminstatus!=20 AND cid = $typeid  AND created<='$nowtimes'  ORDER BY  rand() DESC LIMIT 0 , 6";
	$query = $DEVDB->query($sql);
	while(@$softrow = $DEVDB->fetch_array($query)) {

			$baseIcon = dirname($softrow['icon']).'/litpicH_'.basename($softrow['icon']);
			$iconDir = $GLOBALS['UPLOAD_DIR'].$baseIcon;
			if(is_file($iconDir)){
				$showlogo = GetRandImages($baseIcon);
			}else{
				$showlogo = GetRandImages($softrow['icon']);
			}
			$versionname = $softrow['versionname'];
			$title = $softrow['title'];
			$id = $softrow['id'];

			$recommendlisthtml.='<li>
			  <a class="img72_72" title="'.$title.' V'.$versionname.'" target="_blank" href="http://www.mumayi.com/android-'.$id.'.html">
				  <img alt="'.$title.' V'.$versionname.'"  width="72" height="72" src="'.$showlogo.'">
				  <div class="fff72_72"></div>
			  </a>
			  <div class="tit50">
				  <a title="'.$title.' V'.$versionname.'" target="_blank" href="http://www.mumayi.com/android-'.$id.'.html">'.$title.' V'.$versionname.'</a>
			  </div>
			</li>';

	}
	
	$nowtimes = time();
	//编辑最近更新的精华
	/*
	$sql=" Select
		dede_addonsoft.*,
		dede_archives.*
		From dede_archives
		left join dede_addonsoft on dede_addonsoft.aid=dede_archives.id
		where arcrank=0 AND dede_archives.pubdate<='$nowtimes' AND dede_archives.flag LIKE '%f%' ORDER BY dede_archives.pubdate DESC   limit 0,6 ";
	$result = $DB->query($sql);
	
	$showlogo='';
	*/
	$sql = "SELECT versionname,id,title,icon FROM `mumayi_soft` WHERE sid =0 AND status =2 AND   adminstatus!=20 AND  attribute LIKE '%f%' ORDER BY   created  DESC LIMIT 0 , 6";
	$query = $DEVDB->query($sql);
	while(@$softrow = $DEVDB->fetch_array($query)) {

			$baseIcon = dirname($softrow['icon']).'/litpicH_'.basename($softrow['icon']);
			$iconDir = $GLOBALS['UPLOAD_DIR'].$baseIcon;
			if(is_file($iconDir)){
				$showlogo = GetRandImages($baseIcon);
			}else{
				$showlogo = GetRandImages($softrow['icon']);
			}
			$versionname = $softrow['versionname'];
			$title = $softrow['title'];
			$id = $softrow['id'];

			$recommendlisthtml.='<li>
			  <a class="img72_72" title="'.$title.' V'.$versionname.'" target="_blank" href="http://www.mumayi.com/android-'.$id.'.html">
				  <img alt="'.$title.' V'.$versionname.'"  width="72" height="72" src="'.$showlogo.'">
				  <div class="fff72_72"></div>
			  </a>
			  <div class="tit50">
				  <a title="'.$title.' V'.$versionname.'" target="_blank" href="http://www.mumayi.com/android-'.$id.'.html">'.$title.' V'.$versionname.'</a>
			  </div>
			</li>';

	}


	//写入redis,并设定时间
	
	$dedeCommon->addTimeSet($redis_id,$exptime,$recommendlisthtml);

	$result = file_put_contents($filenames, $recommendlisthtml);
	@chmod($filenames, 0775);

}else{
	$recommendlisthtml = $dedeCommon->addGet($redis_id);
	
}

$dataArr['recommendList'] = $recommendlisthtml.$bottommpkhtml;



//Right右侧广告
$redis_id = "softleftfocusad";
$result = $dedeCommon->addGet($redis_id);
$result = '';
//先取到焦点图片
if($result==''){
	$sql="SELECT *  FROM `dede_focusimages` WHERE `cooper` LIKE '%softright%' AND adtype=3 order by pxid desc";
	$result = $DB->query($sql);
	$fouceAD = '';
	while($softrow = $DB->fetch_array($result)) {
			$focusAD.='<a href="'.$softrow['url'].'" title="'.$softrow['title'].'" target="_blank" ><img alt="'.$softrow['title'].'" width="270" height="130"  src="'.$softrow['imgurls'].'" ></a>';
	}
	$exptime = 3600; 
	//写入redis,并设定时间
	$dedeCommon->addTimeSet($redis_id,$exptime,$fouceAD);


}else{
	$focusAD = $dedeCommon->addGet($redis_id);
	
}

$dataArr['focusList'] = $focusAD;

//取到正方形广告图片
$redis_id = "softleftonead";
$result = $dedeCommon->addGet($redis_id);
$result = '';
if($result==''){
	$sql="SELECT *  FROM `dede_focusimages` WHERE `cooper` LIKE '%softright%' AND adtype=2 order by pxid desc";
	$result = $DB->query($sql);
	while($softrow = $DB->fetch_array($result)) {
			if($softrow['isflash']){
				$rightoneAD[]='<object width="280" height="80" align="middle" id="'.$softrow['title'].'" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"><param value="never" name="allowScriptAccess"><param value="high" name="quality"><param value="transparent" name="wmode"><param value="'.$softrow['imgurls'].'" name="movie"><embed width="280" height="80" align="middle" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="never" quality="high" name="'.$softrow['title'].'" src="'.$softrow['imgurls'].'" wmode="transparent"></object>';
			}else{
				$rightoneAD[]='<a href="'.$softrow['url'].'" title="'.$softrow['title'].'" target="_blank" ><img src="'.$softrow['imgurls'].'" width="250" height="250" alt="'.$softrow['title'].'"></a>';
			}
	}
	$exptime = 3600; 
	//写入redis,并设定时间
	$dedeCommon->addTimeSet($redis_id,$exptime,serialize($rightoneAD));


}else{
	$rightoneAD = unserialize($dedeCommon->addGet($redis_id));
	
}

$dataArr['showbigpic0'] = $rightoneAD[0];
$dataArr['showbigpic1'] = $rightoneAD[1];



//取到长方形广告图片
$redis_id = "softlefttwoad";
$result = $dedeCommon->addGet($redis_id);
$result = '';
if($result==''){
	$sql = "SELECT *  FROM `dede_focusimages` WHERE `cooper` LIKE '%softright%' AND adtype=1 order by pxid desc";
	$result = $DB->query($sql);
	while($softrow = $DB->fetch_array($result)) {
		if($softrow['isflash']){
			$righttwoAD[]='<object width="280" height="80" align="middle" id="'.$softrow['title'].'" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"><param value="never" name="allowScriptAccess"><param value="high" name="quality"><param value="transparent" name="wmode"><param value="'.$softrow['imgurls'].'" name="movie"><embed width="280" height="80" align="middle" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="never" quality="high" name="'.$softrow['title'].'" src="'.$softrow['imgurls'].'" wmode="transparent"></object>';
		}else{
			$righttwoAD[]='<a href="'.$softrow['url'].'" title="'.$softrow['title'].'" target="_blank" ><img src="'.$softrow['imgurls'].'" width="280" height="80" alt="'.$softrow['title'].'"></a>';
		}
	}
	$exptime = 3600; 
	//写入redis,并设定时间
	$dedeCommon->addTimeSet($redis_id,$exptime,serialize($righttwoAD));

}else{
	$righttwoAD = unserialize($dedeCommon->addGet($redis_id));
}

$dataArr['showpic0'] = $righttwoAD[0];
$dataArr['showpic1'] = $righttwoAD[1];
$dataArr['showpic2'] = $righttwoAD[2];





//Right右侧新闻
//缓存ID
$redis_id="rightarticlelist";
$result = $dedeCommon->addGet($redis_id);
$rightarticlehtml = '';
//如果过期，重新生成一次，再写入缓存
$result= '';
if($result==''){

	$sql="Select `id`,`title`,`description`,`senddate`,`arcrank`,`litpic`,`color` From
                `dede_archives`  WHERE dede_archives.arcrank =0 AND dede_archives.channel=1 AND dede_archives.flag LIKE '%h%' ORDER BY `id` DESC limit 5";
	$sresult = $WebDB->query($sql);
	$showlogo='';
	$i = 0;
	while($softrow = $WebDB->fetch_array($sresult)) {
			$showlogo = $softrow['litpic'];
			$aurl = 'http://news.mumayi.com/article-'.date('Y',$softrow['senddate']).'-'.date('md',$softrow['senddate']).'-'.$softrow['id'].'.html';
			if($softrow['color']){
				$rightarticlehtml.='<li><a href="'.$aurl.'" target="_blank" title="'.$softrow['title'].'"><font color="'.$softrow['color'].'">'.$softrow['title'].'</font></a></li>';
			}else{
				$rightarticlehtml.='<li><a href="'.$aurl.'" target="_blank" title="'.$softrow['title'].'">'.$softrow['title'].'</a></li>';
			}

			$i++;

	}

	$exptime = 3600;
	//写入redis,并设定时间
	$dedeCommon->addTimeSet($redis_id,$exptime,$rightarticlehtml);

}else{
	$rightarticlehtml = $dedeCommon->addGet($redis_id);

}
$dataArr['rightarticle'] = $rightarticlehtml;




function FormatDownNum($downnum = 0) {
	if($downnum > 100000) {
		return intval($downnum / 10000).'万+';
	} else {
		return $downnum;
	}
}


echo json_encode($dataArr);


exit;